package com.wyp.didishige.activity.dynamic;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.wyp.didishige.MyApplication;
import com.wyp.didishige.activity.BaseActivity;
import com.wyp.didishige.fragment.photo.PhotoFragment;
import com.wyp.didishige.model.photo.PhotoInfo;
import com.wyp.didishige.utils.photo.CheckImageLoaderConfiguration;
import com.wyp.didishige.R;
import com.wyp.didishige.fragment.photo.PhotoFolderFragment;
import com.wyp.didishige.model.photo.PhotoSerializable;

import java.util.ArrayList;
import java.util.List;

/**
 * @author guilin
 * @title SelectPhotoActivity.java
 * @package com.centaline.mhc.activity.friendGroupActivity
 * @date 2013-8-6 上午10:42:15
 */
public class SelectPhotoActivity extends BaseActivity implements PhotoFolderFragment.OnPageLodingClickListener
        , PhotoFragment.OnPhotoSelectClickListener {

    private PhotoFolderFragment photoFolderFragment;

    private ArrayList<PhotoInfo> hasList;

    private FragmentManager manager;
    private int backInt = 0;
    /**
     * 可选择图片数量
     */
    private int count;
    Button textView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selectphoto);

        actionBar=getSupportActionBar();
        actionBar.setTitle("选择图片");
        actionBar.setDisplayHomeAsUpEnabled(true);

        getWindowManager().getDefaultDisplay().getMetrics(MyApplication.getDisplayMetrics());

        count = 8 - getIntent().getIntExtra("count", 0);

        manager = getSupportFragmentManager();

        hasList = new ArrayList<PhotoInfo>();
        textView=new Button(this);

        textView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                initResult();
            }
        });
        photoFolderFragment = new PhotoFolderFragment();

        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.body, photoFolderFragment);
        transaction.addToBackStack(null);
        // Commit the transaction
        transaction.commit();
    }

    @Override
    protected void onStart() {
        super.onStart();
        CheckImageLoaderConfiguration.checkImageLoaderConfiguration(this);
    }

    @Override
    public void onPageLodingClickListener(List<PhotoInfo> list) {
        FragmentTransaction transaction = manager.beginTransaction();
        PhotoFragment photoFragment = new PhotoFragment();
        Bundle args = new Bundle();
        PhotoSerializable photoSerializable = new PhotoSerializable();
        for (PhotoInfo photoInfoBean : list) {
            photoInfoBean.setChoose(false);
        }
        photoSerializable.setList(list);
        args.putInt("count", count);
        args.putSerializable("bmobIMMessageList", photoSerializable);
        photoFragment.setArguments(args);
        transaction = manager.beginTransaction();
        transaction.hide(photoFolderFragment).commit();
        transaction = manager.beginTransaction();
        transaction.add(R.id.body, photoFragment);
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        transaction.addToBackStack(null);
        // Commit the transaction
        transaction.commit();
        backInt++;
    }

    @Override
    public void onPhotoSelectClickListener(List<PhotoInfo> list) {
        // TODO Auto-generated method stub
        hasList.clear();
        for (PhotoInfo photoInfoBean : list) {
            if (photoInfoBean.isChoose()) {
                hasList.add(photoInfoBean);
            }
        }
        supportInvalidateOptionsMenu();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            if (backInt == 0) {
                finish();
            } else if (backInt == 1) {
                backInt--;
                hasList.clear();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.show(photoFolderFragment).commit();
                manager.popBackStack(0, 0);
            }
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initResult() {
        Bundle bundle = new Bundle();
        bundle.putSerializable("hasList", hasList);
        Intent intent = new Intent(SelectPhotoActivity.this, NewDynamicActivity.class);
        intent.putExtras(bundle);
        SelectPhotoActivity.this.setResult(RESULT_OK, intent);
        SelectPhotoActivity.this.finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();

        inflater.inflate(R.menu.menu_select_photo, menu);

        MenuItem item = menu.findItem(R.id.action_num);

        textView.setBackgroundResource(R.drawable.blue_bg_selector);

        if (hasList != null) {
            textView.setText("发送(" + hasList.size() + "/"+count+")");
        } else {
            textView.setText("发送(0/"+count+")");
        }
        textView.setTextColor(Color.WHITE);

        textView.setPadding(20, 20, 20, 20);
        item.setActionView(textView);
        actionBar.setCustomView(textView,
                new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.MATCH_PARENT, Gravity.RIGHT));
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void onBackPressed() {
        if (backInt == 0) {
            finish();
        } else if (backInt == 1) {
            backInt--;
            hasList.clear();
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.show(photoFolderFragment).commit();
            manager.popBackStack(0, 0);
        }
    }
}
